package com.thinking.live.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thinking.live.entity.LiveProductDO;
import com.thinking.live.entity.pojo.req.LivingCostMiniReq;
import com.thinking.live.entity.pojo.req.LivingCostReq;
import com.thinking.live.entity.pojo.req.RevocationReq;
import com.thinking.live.service.LiveChargeService;
import com.thinking.live.service.impl.LiveProductServiceImpl;
import com.thinking.live.vo.dto.LiveChargeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 @author tangsixiang@hotmail.com
 描述：生活缴费业务处理模块
 @date 2023年12月01日 14:28

 */

@RestController
@RequestMapping("/livingCosts")
public class LivingBusinessController {

    @Autowired
    LiveChargeService liveChargeService;

    @Autowired
    LiveProductServiceImpl productService;
    /**
     *  用户下单进行生活缴费充值业务
     * @return
     */
    @PostMapping("/queryDayLimit")
    public Map<Object,Object>  queryDayLimit() {
        return liveChargeService.queryDayLimit();
    }


    /**
     * 查询支持充电费地区
     * @return
     */
    @PostMapping("/queryDianFeiArea")
    public String queryDianFeiArea() {

      return  liveChargeService.queryDianFeiArea();
    }

    /***
     * 查询充值中心缴费类型产品列表 -add huafei  dianfei huiyuan
     * @param costReq
     * @return
     */
    @PostMapping("/queryLivingType")
    public List<Map<String, Object>> queryLivingType( @RequestBody LivingCostReq costReq) {
        QueryWrapper<LiveProductDO> eq = new QueryWrapper<LiveProductDO>().eq("product_type", costReq.getType()).eq("product_hide", 0);
        return   productService.listMaps(eq);

    }
    @PostMapping("/queryLivingStatus")
    public LiveChargeDTO queryLivingStatus( @RequestBody LivingCostReq costReq ) {
        return BeanUtil.toBean(liveChargeService.queryLivingStatus(costReq.getLiveId()), LiveChargeDTO.class);

    }

    /**
     *  用户下单进行生活缴费充值业务
     * @param costReq
     * @return
     */
    @PostMapping("/makeOrder")
    public Map<String, Object> makeOrder(  @RequestBody LivingCostReq  costReq) throws Exception {

          return liveChargeService.makeLivingChargeData(costReq);
     }


    /**
     * 电费缴纳功能
     * @param costReq
     * @return
     * @throws Exception
     */
    @PostMapping("/makeOrderElecity")
    public Map<String, Object> makeOrderElecity( @RequestBody LivingCostReq  costReq) throws Exception {

        return liveChargeService.makeLivingChargeData(costReq);
    }

    /***
     * 首次充值失败用户申请二次提交充值订单-便于增加成功率
     * @param request
     * @return
     */
    @PostMapping("/doubleOrder")
    public Map<String,Object> doubleOrder(@RequestBody RevocationReq request) {
         Map<String,Object> map=new HashMap<>();
         map.put("status", liveChargeService.dubbleSendOrderNo(request.getOrderNo().substring(2))?1:0);
         return map;
    }
    /**
     *  用户下单进行生活缴费充值业务(支付宝小程序)
     * @param costMiniReq
     * @return
     */
    @PostMapping("/makeMiniOrder")
    public Map<String, Object> makeMiniOrder(@RequestBody LivingCostMiniReq costMiniReq) throws Exception {
        return liveChargeService.makeMiniLivingChargeData(costMiniReq);
    }

    /**
     *
     * @param request
     * @return
     */
    @PostMapping("/requestRefundOrder")
    public Map<String,Object> requestRefundOrder(@RequestBody RevocationReq request) {
        Map<String,Object> map=new HashMap<>();
        map.put("status", liveChargeService.refundChargeOrderNo(request.getOrderNo()));
        return map;
    }

}
